#include <d3dx9.h>
#include "utils.h"
#define PI 3.14159265

LPDIRECT3DSURFACE9 CreateSurfaceFromFile(LPDIRECT3DDEVICE9 d3ddv, LPWSTR FilePath)
{
	D3DXIMAGE_INFO info; 

	HRESULT result = D3DXGetImageInfoFromFile(FilePath, &info);
	if (result!=D3D_OK)
	{
		return NULL;
	}

	LPDIRECT3DSURFACE9 surface;

	d3ddv->CreateOffscreenPlainSurface(
			info.Width,				// width
			info.Height,			// height
			D3DFMT_X8R8G8B8,		// format
			D3DPOOL_DEFAULT,		
			&surface,
			NULL);

	result = D3DXLoadSurfaceFromFile(
			surface, 		// surface
			NULL,			// destination palette	
			NULL,			// destination rectangle 
			FilePath,			
			NULL,			// source rectangle
			D3DX_DEFAULT, 	// filter image
			D3DCOLOR_XRGB(0,0,0),				// transparency (0 = none)
			NULL);			// reserved

	if (result!=D3D_OK)
	{
		return NULL;
	}

	return surface;
}

double GetAngle(D3DXVECTOR2 a, D3DXVECTOR2 b)
{
	double angle;
	if(a.y == b.y)
		angle = 0;
	else
		angle = atan(abs(a.x-b.x)/abs(a.y-b.y))*180/PI;
	if(a.x >= b.x && a.y >= b.y)
		return 90 - angle;
	if(a.x < b.x && a.y >= b.y)
		return angle + 90;
	if(a.x <= b.x && a.y < b.y)
		return 90 - angle + 180;
	if(a.x > b.x && a.y < b.y)
		return angle + 270;
}

int Next(int Current, int Start, int End)
{
	Current++;
	if (Current < 0 || Current > End)
		Current = 0;
	return Current;
}

int Previous(int Current, int Start, int End)
{
	Current--;
	if (Current < 0 || Current > End)
		Current = End;
	return Current;
}